﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Antix.Data.Catalogue.Objects
{
    /// <summary>
    /// Database Table Reference Collection
    /// </summary>
    public class DatabaseReferenceCollection : DatabaseObjectCollection
    {
        #region Constructor

        /// <summary>
        /// Create collection
        /// </summary>
        internal DatabaseReferenceCollection(Database database)
        {
            Database = database;
        }

        #endregion

        #region Properties

        public Database Database { get; private set; }

        #region Indexers

        ///// <summary>
        ///// Get item by name, if not found a new one is passed back
        ///// </summary>
        ///// <param name="name">Name</param>
        ///// <returns>Database</returns>
        //new public DatabaseReference this[string name]
        //{
        //    get { return this[name, false]; }
        //}

        /// <summary>
        /// Get item by name, if not found a new one is passed back
        /// </summary>
        /// <param name="name">Name</param>
        /// <param name="create">Create if not found</param>
        /// <returns>Database</returns>
        public DatabaseReference this[string name, bool create]
        {
            get
            {
                if (this.Contains(name)) { return (DatabaseReference)base[name]; }
                else if (create) { return this.Add(name); }
                else { return null; }
            }
        }

        #endregion

        #endregion

        #region Methods

        #region Add

        /// <summary>
        /// Add a new reference
        /// </summary>
        /// <param name="name">Column name</param>
        /// <returns>New reference</returns>
        public DatabaseReference Add(string name)
        {
            DatabaseReference newItem = new DatabaseReference(this.Database);
            newItem.Name = name;

            base.Add(newItem);

            return newItem;
        }

        /// <summary>
        /// Add Existing item to collection
        /// </summary>
        /// <param name="item"></param>
        internal void Add(DatabaseReference item)
        {
            base.Add(item);
        }

        #endregion

        #endregion
    }
}
